﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DataAccess;
using Entity;
namespace StudentAttendanceChecking.Forms
{
    public partial class Form1 : Form
    {
        public void fillComboboxClass()
        {
            using (SqlConnection conn = ConnectData.getConnection())
            {
                try
                {
                    //SqlConnection con = new SqlConnection("Data Source=ANHNT-PC;Initial Catalog=StudentAttendanceChecking;Integrated Security=True");
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter("Select * from Class where ClassStatus=1", conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    cboClass.DataSource = ds.Tables[0];
                    cboClass.DisplayMember = "ClassName";
                    conn.Close();

                }
                catch (Exception e)
                {

                }

            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillComboboxClass();
        }

        private void cboClass_SelectedValueChanged(object sender, EventArgs e)
        {
            SqlConnection conn = ConnectData.getConnection();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select Subjects.SubjectName from Subjects inner join Attendants on Attendants.SubjectCode= Subjects.SubjectCode inner join Class on Attendants.ClassID= Class.ClassID	where Class.ClassName like "+"'"+cboClass.Text+"'",conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cboStudent.DataSource = ds.Tables[0];
            cboStudent.DisplayMember = "SubjectName";
            conn.Close();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            SqlConnection conn = ConnectData.getConnection();
            conn.Open();
            SqlCommand cmd = new SqlCommand("spViewStudent", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ClassID", SqlDbType.Int);
            Classs class1 = new Classs();
            class1=(Classs)cboClass.SelectedItem;
            cmd.Parameters["@ClassID"].Value = class1.ClassID;
            MessageBox.Show(class1.ClassID.ToString());
            Subjects sub = new Subjects();
            cmd.Parameters.Add("@SubjectCode",SqlDbType.VarChar);
            cmd.Parameters["@SubjectCode"].Value = sub.SubjectCode;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            grvViewStudent.DataSource = ds.Tables[0];
        }

    }
}
